package gclearn;


public class FinalizeTest {
	
	public static FinalizeTest object;
	
	public void isAlive() {
		System.out.println("i'm alive");
	}
	
	@Override
	protected void finalize() throws Throwable{
		super.finalize();
		object = this;
		System.out.println("method finalize done.");
	}
	
	public static void main(String[] args) throws InterruptedException {
		
		object = new FinalizeTest();
		
		//第一次执行，finalize方法会自救
		object = null;
		System.gc();
		
		Thread.sleep(500);
		if(object != null) {
			object.isAlive();
		}else {
			System.out.println("i'am died.");
		}
		
		//finalize之前已执行过
		object = null;
		System.gc();
		
		Thread.sleep(500);
		if(object != null) {
			object.isAlive();
		}else {
			System.out.println("i'am died.");
		}
		
		
	}

}
